using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame4
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public enum GameState
        {
            MainMenu,
            TutorialMenu,
            About,
            PlayGame,
            PlayGameAdv,
            CompGame,
            CompGameAdv,
            GameOver,
            Reset,
            Solving,
        }
        public GameState gameState;

        public Texture2D MenuTexture;

        GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;
        public MouseState mouseState;
        public List<Movable> movable;
        public Solver solver;
        /*public Texture2D[] movableTexture;
        public SpriteManager[] movableSM;
        public readonly static string[] movableTexturePath = new string[]
        {
            "car2h2",
            "car3h2",
            "car2v2",
            "car3v2"
        };*/
        public Field field = new Field(6, 6);
        public Movable mov, mov1;

        public SpriteManager terrain;
        public SpriteManager obj_h2, obj_v2, obj_h3, obj_v3, obj_main;
        private int i, j;
        private bool kunci = false;
        private Random rand;
        public int roomType;
        float frameRate = 1000 / 10;
        Double frameTimer = 0f;
        public int curFrame = 0;
        Boolean aftermath;

        public SpriteFont font;
        public string time;
        public TimeSpan eTime;
        public int moveCount;

        public int now;
        public float x, y;
        public int arah;
        public bool locking;
        public bool mentok = false;
        public int speed = 1;

        public Button playerMode, computerMode, help, about, easy, medium, hard, bfs, dfs;
        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 825;
            graphics.PreferredBackBufferHeight = 675;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        /// 
        private void removeall()
        {
            for (int i = 0; i < Components.Count; i++)
            {
                Components.RemoveAt(i);
                i--;
            }
            for (int i = 0; i < movable.Count; i++)
            {
                movable.RemoveAt(i);
                i--;
            }
            //Initialize();
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            field.printField();
            gameState = GameState.MainMenu;
            Random random = new Random();

            roomType = random.Next(0, 3);
            aftermath = false;
            moveCount = 0;
            time = "";
            font = Content.Load<SpriteFont>("SpriteFont1");

            IsMouseVisible = true;
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        /// 

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            MenuTexture = Content.Load<Texture2D>("bg");
            terrain = new SpriteManager(Content.Load<Texture2D>("terrain2"), 2, 8);
            obj_h2 = new SpriteManager(Content.Load<Texture2D>("obj_h2"), 1, 9);
            obj_v2 = new SpriteManager(Content.Load<Texture2D>("obj_v2"), 9, 1);
            obj_h3 = new SpriteManager(Content.Load<Texture2D>("obj_h3"), 1, 9);
            obj_v3 = new SpriteManager(Content.Load<Texture2D>("obj_v3"), 9, 1);
            obj_main = new SpriteManager(Content.Load<Texture2D>("dog"), 8, 1);
            playerMode = new Button(new Vector2(507, 50), Content.Load<Texture2D>("banner"), 1, font, "Player Mode");
            computerMode = new Button(new Vector2(507, 150), Content.Load<Texture2D>("banner"), 2, font, "Computer Mode");
            help = new Button(new Vector2(507, 250), Content.Load<Texture2D>("banner"), 3, font, "Help");
            about = new Button(new Vector2(507, 350), Content.Load<Texture2D>("banner"), 4, font, "About");
            easy = new Button(new Vector2(507, 150), Content.Load<Texture2D>("banner"), 5, font, "Easy");
            medium = new Button(new Vector2(507, 250), Content.Load<Texture2D>("banner"), 6, font, "Medium");
            hard = new Button(new Vector2(507, 350), Content.Load<Texture2D>("banner"), 7, font, "Hard");
            bfs = new Button(new Vector2(507, 250), Content.Load<Texture2D>("banner"), 6, font, "BFS");
            dfs = new Button(new Vector2(507, 350), Content.Load<Texture2D>("banner"), 7, font, "DFS");
            
            //define the scaling
            float ScaleByWidth = (float)GraphicsDevice.PresentationParameters.BackBufferHeight / (float)((terrain.GetTexture().Width / terrain.GetMaxFrameH()) * (field.GetMaxCol() + 2));
            float ScaleByHeight = (float)GraphicsDevice.PresentationParameters.BackBufferHeight / (float)((terrain.GetTexture().Height / terrain.GetMaxFrameV()) * (field.GetMaxRow() + 3));

            //assign what scaling to be applied
            if (field.GetMaxCol() > field.GetMaxRow())
            {
                terrain.Scale = ScaleByWidth;
                obj_h2.Scale = ScaleByWidth;
                obj_v2.Scale = ScaleByWidth;
                obj_h3.Scale = ScaleByWidth;
                obj_v3.Scale = ScaleByWidth;
                obj_main.Scale = ScaleByWidth;
            }
            else
            {
                terrain.Scale = ScaleByHeight;
                obj_h2.Scale = ScaleByHeight;
                obj_v2.Scale = ScaleByHeight;
                obj_h3.Scale = ScaleByHeight;
                obj_v3.Scale = ScaleByHeight;
                obj_main.Scale = ScaleByHeight;
            }

            rand = new Random();
            movable = new List<Movable>();
            mov = new Movable(this, 0, new Vector2(terrain.tileWidth * terrain.Scale, terrain.tileHeight * terrain.Scale * 3), obj_main, 2);

            movable.Add(mov);
            Components.Add(mov);
            int baris = 0, kolom = 5;
            int kol2 = 0, kol3 = 0, kol4 = 0, kol5 = 0;
            while (baris < 6)
            {
                if (rand.Next(0, 2) == 0 && baris != 1)
                {
                    int x = rand.Next(0, 2);
                    if (x == 0)
                    {
                        mov1 = new Movable(this, 0, new Vector2((kolom + 1) * (terrain.tileWidth * terrain.Scale), (baris + 2) * (terrain.tileHeight * terrain.Scale)), obj_h2, 1);
                    }
                    else
                    {
                        mov1 = new Movable(this, 0, new Vector2((kolom + 1) * (terrain.tileWidth * terrain.Scale), (baris + 2) * (terrain.tileHeight * terrain.Scale)), obj_h3, 1);
                    }
                    movable.Add(mov1);
                    if (mov1.isCollision() || mov1.outofmap())
                    {
                        movable.Remove(mov1);
                        //Console.WriteLine("delete");
                    }
                    else
                    {
                        Components.Add(mov1);
                    }
                }
                else
                {
                    int x = rand.Next(2, 4);
                    if (x == 2)
                    {
                        mov1 = new Movable(this, 1, new Vector2((kolom + 1) * (terrain.tileWidth * terrain.Scale), (baris + 2) * (terrain.tileHeight * terrain.Scale)), obj_v2, 1);
                    }
                    else
                    {
                        mov1 = new Movable(this, 1, new Vector2((kolom + 1) * (terrain.tileWidth * terrain.Scale), (baris + 2) * (terrain.tileHeight * terrain.Scale)), obj_v3, 1);
                    }
                    movable.Add(mov1);
                    if (mov1.isCollision() || mov1.outofmap())
                    {
                        movable.Remove(mov1);
                        //Console.WriteLine("delete");
                    }
                    else
                    {
                        switch (kolom)
                        {
                            case 0: { Components.Add(mov1); break; }
                            case 1: { Components.Add(mov1); break; }
                            case 2: { if (x == 3) kol2 += 2; else kol2++; if (kol2 <= 2) Components.Add(mov1); else movable.Remove(mov1); break; }
                            case 3: { if (x == 3) kol3 += 2; else kol3++; if (kol3 <= 2) Components.Add(mov1); else movable.Remove(mov1); break; }
                            case 4: { if (x == 3) kol4 += 2; else kol4++; if (kol4 <= 2) Components.Add(mov1); else movable.Remove(mov1); break; }
                            case 5: { if (x == 3) kol5 += 2; else kol5++; if (kol5 <= 2) Components.Add(mov1); else movable.Remove(mov1); break; }
                        }
                    }
                }

                mov1.randObj = rand.Next(0, 3);
               
                kolom--;
                if (kolom < 0)
                {
                    kolom = 5; baris++;
                }
                //solver = new Solver(this.movable, field.GetMaxCol(), field.GetMaxRow());
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            
            switch (gameState)
            {
                case (GameState.PlayGame):
                    {
                        frameTimer += gameTime.ElapsedGameTime.TotalMilliseconds;

                        if (eTime.Seconds > 0)
                        {
                            if (!aftermath)
                            {
                                eTime -= gameTime.ElapsedGameTime;
                                if (eTime.Minutes < 10)

                                    time = "0" + (int)(eTime.Minutes);
                                else
                                    time = "" + (int)(eTime.Minutes);
                                if (eTime.Seconds < 10)
                                    time += ":0" + (int)(eTime.Seconds);
                                else
                                    time += ":" + (int)(eTime.Seconds);
                            }
                        }
                        else
                        {
                            removeall();
                            Initialize();
                            gameState = GameState.MainMenu;
                        }

                        if (frameTimer > frameRate)
                        {
                            frameTimer = 0f;
                            curFrame = (curFrame + 1) % 5;
                        }

                        if (field.GetGrid()[1, 5] == 2)
                        {
                            removeall();
                            Initialize();
                        }
                        playinput();
                        base.Update(gameTime);

                        break;
                    }
                case (GameState.MainMenu):
                    {
                        

                        if (playerMode.isSelected(Mouse.GetState().X, Mouse.GetState().Y) && (Mouse.GetState().LeftButton == ButtonState.Pressed))
                        {
                            gameState = GameState.PlayGameAdv;

                        }
                        else if (computerMode.isSelected(Mouse.GetState().X, Mouse.GetState().Y) && (Mouse.GetState().LeftButton == ButtonState.Pressed))
                        {
                            gameState = GameState.CompGameAdv;
                        }
                        else if (help.isSelected(Mouse.GetState().X, Mouse.GetState().Y) && (Mouse.GetState().LeftButton == ButtonState.Pressed))
                        {
                            gameState = GameState.TutorialMenu;
                        }
                        else if (about.isSelected(Mouse.GetState().X, Mouse.GetState().Y) && (Mouse.GetState().LeftButton == ButtonState.Pressed))
                        {
                            gameState = GameState.About;
                        }
                        break;
                    
                        /*Solver _solver = new Solver(this.movable, field.GetMaxCol(), field.GetMaxRow());
                        if (Keyboard.GetState().IsKeyDown(Keys.Space))
                        {
                            while (!_solver.Solve(0))
                            {
                                removeall();
                                LoadContent();
                                _solver = new Solver(this.movable, field.GetMaxCol(), field.GetMaxRow());
                            }
                            gameState = GameState.PlayGame;
                        }
                        break;*/
                    }
                case (GameState.PlayGameAdv):
                    {
                        if (easy.isSelected(Mouse.GetState().X, Mouse.GetState().Y) && (Mouse.GetState().LeftButton == ButtonState.Pressed))
                        {
                            gameState = GameState.PlayGame;
                            eTime = TimeSpan.FromSeconds(90);

                        }
                        else if (medium.isSelected(Mouse.GetState().X, Mouse.GetState().Y) && (Mouse.GetState().LeftButton == ButtonState.Pressed))
                        {
                            gameState = GameState.PlayGame;
                            eTime = TimeSpan.FromSeconds(30);

                        }
                        else if (hard.isSelected(Mouse.GetState().X, Mouse.GetState().Y) && (Mouse.GetState().LeftButton == ButtonState.Pressed))
                        {
                            gameState = GameState.PlayGame;
                            eTime = TimeSpan.FromSeconds(10);

                        }
                        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                        {
                            gameState = GameState.MainMenu;
                        }
                        break;
                    }
                case (GameState.CompGameAdv):
                    {
                        if (bfs.isSelected(Mouse.GetState().X, Mouse.GetState().Y) && (Mouse.GetState().LeftButton == ButtonState.Pressed))
                        {
                            gameState = GameState.CompGame;
                        }
                        else if (dfs.isSelected(Mouse.GetState().X, Mouse.GetState().Y) && (Mouse.GetState().LeftButton == ButtonState.Pressed))
                        {
                            gameState = GameState.CompGame;
                        }
                        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                        {
                            gameState = GameState.MainMenu;
                        }
                        break;
                    }
                case (GameState.Reset): 
                    {
                        removeall();
                        gameState = GameState.PlayGame;
                        LoadContent();
                        break;
                    }
                case (GameState.CompGame):
                    {
                        solver = new Solver(this.movable, field.GetMaxCol(), field.GetMaxRow());
                        while (!solver.Solve(0))
                        {
                            removeall();
                            LoadContent();
                            solver = new Solver(this.movable, field.GetMaxCol(), field.GetMaxRow());
                        }
                        
                        
                        locking = false;
                        gameState = GameState.Solving;
                        


                        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                        {
                            gameState = GameState.MainMenu;
                        }
                        break;
                    }
                case (GameState.About):
                    {
                        gameState = GameState.About;
                        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                        {
                            gameState = GameState.MainMenu;
                        }
                        break;
                    }
                case (GameState.TutorialMenu):
                    {
                        gameState = GameState.TutorialMenu;
                        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                        {
                            gameState = GameState.MainMenu;
                        }
                        break;
                    }
                case (GameState.Solving):
                    {
                        
                        frameTimer += gameTime.ElapsedGameTime.TotalMilliseconds;

                        if (frameTimer > frameRate)
                        {
                            frameTimer = 0f;
                            curFrame = (curFrame + 1) % 5;
                        }

                        if (field.GetGrid()[1, 5] == 2)
                        {
                            removeall();
                            Initialize();
                        }
                        //playinput();
                        //solving();
                        if (Keyboard.GetState().IsKeyDown(Keys.H))
                        {
                            speed++;
                            if (speed > 5) { speed=5; }
                        }
                        if (Keyboard.GetState().IsKeyDown(Keys.J))
                        {
                            speed--;
                            if (speed < 1) { speed=1; }
                        }

                        if ((solver.path.Count) > 0 && !locking)
                        {
                            now = solver.path.Pop();

                            arah = solver.visited[now].getDir();
                            locking = true;
                            Console.WriteLine("mobil ke " + solver.visited[now].getMov() + " " + arah + " " + " pos " + movable[solver.visited[now].getMov()].position.X + " " + movable[solver.visited[now].getMov()].position.Y);
                        }
                        else
                        {
                            switch (arah)
                            {
                                case 1: { y = movable[solver.visited[now].getMov()].moveto.Y - speed; x = movable[solver.visited[now].getMov()].position.X; break; }
                                case 2: { x = movable[solver.visited[now].getMov()].moveto.X + speed; y = movable[solver.visited[now].getMov()].position.Y; break; }
                                case 3: { y = movable[solver.visited[now].getMov()].moveto.Y + speed; x = movable[solver.visited[now].getMov()].position.X; break; }
                                case 4: { x = movable[solver.visited[now].getMov()].moveto.X - speed; y = movable[solver.visited[now].getMov()].position.Y; break; }
                            }
                            Console.WriteLine(x + " " + y);
                            movable[solver.visited[now].getMov()].moveto.Y = y;
                            movable[solver.visited[now].getMov()].moveto.X = x;

                            base.Update(gameTime);
                            if (mentok)
                            {
                                /*switch (arah)
                                {
                                    case 1: { movable[solver.visited[now].getMov()].moveto.Y = y - 9; movable[solver.visited[now].getMov()].moveto.X = x; break; }
                                    case 2: { movable[solver.visited[now].getMov()].moveto.X = x + 9; movable[solver.visited[now].getMov()].moveto.Y = y; break; }
                                    case 3: { movable[solver.visited[now].getMov()].moveto.Y = y + 9; movable[solver.visited[now].getMov()].moveto.X = x; break; }
                                    case 4: { movable[solver.visited[now].getMov()].moveto.X = x - 9; movable[solver.visited[now].getMov()].moveto.Y = y; break; }
                                         
                                }*/
                                //movable[solver.visited[now].getMov()].moveto.Y = y;
                                //movable[solver.visited[now].getMov()].moveto.X = x;
                                movable[solver.visited[now].getMov()].resetmoveto();
                                mentok = false;
                                locking = false;
                                movable[solver.visited[now].getMov()].moveto.Y = movable[solver.visited[now].getMov()].position.Y;
                                movable[solver.visited[now].getMov()].moveto.X = movable[solver.visited[now].getMov()].position.X;
                            }
                            movable[solver.visited[now].getMov()].moveto.Y = movable[solver.visited[now].getMov()].position.Y;
                            movable[solver.visited[now].getMov()].moveto.X = movable[solver.visited[now].getMov()].position.X;
                            
                        }
                        break;
                    }
              }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            switch (gameState)
            {
                case (GameState.PlayGame):
                    {
                        int i, j;
                        for (i = 0; i < field.GetMaxCol() + 2; i++)
                        {
                            for (j = 0; j < field.GetMaxRow() + 3; j++)
                            {
                                terrain.Position = new Vector2(i * (float)(terrain.GetTexture().Width / terrain.GetMaxFrameH() * terrain.Scale), j * (float)(terrain.GetTexture().Height / terrain.GetMaxFrameV() * terrain.Scale));
                                if (i % 2 == 0)
                                {
                                    if (j == 1)
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 0);
                                    }
                                    else if ((j != 0) && (j != field.GetMaxRow() + 2))
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 1);
                                    }
                                }
                                else
                                {
                                    if (j == 1)
                                    {
                                        terrain.Draw(this.spriteBatch, 1, 0);
                                    }
                                    else if ((j != 0) && (j != field.GetMaxRow() + 2))
                                    {
                                        terrain.Draw(this.spriteBatch, 1, 1);
                                    }
                                }

                                if (i == 0)
                                {
                                    if (j == 0)
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 2);
                                    }
                                    else if (j == field.GetMaxRow() + 2)
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 3);
                                    }
                                    else
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 4);
                                    }
                                }
                                else if (i == field.GetMaxCol() + 1)
                                {
                                    if (j == 3)
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 5);
                                    }
                                    else if (j == 0)
                                    {
                                        terrain.Draw(this.spriteBatch, 1, 2);
                                    }
                                    else if (j == field.GetMaxRow() + 2)
                                    {
                                        terrain.Draw(this.spriteBatch, 1, 3);
                                    }
                                    else
                                    {
                                        terrain.Draw(this.spriteBatch, 1, 4);
                                    }
                                }
                                else if (j == 0)
                                {
                                    terrain.Draw(this.spriteBatch, 1, 5);
                                }
                                else if (j == field.GetMaxRow() + 2)
                                {
                                    terrain.Draw(this.spriteBatch, 1, 6);
                                }
                                if ((i == 6) && (j == 3))
                                {
                                    terrain.Draw(this.spriteBatch, 1, 7);
                                }
                            }
                        }
                        spriteBatch.Draw(Content.Load<Texture2D>("panel"), new Vector2(GraphicsDevice.PresentationParameters.BackBufferWidth - 225, 0), Color.White);
                        spriteBatch.DrawString(font, "Time Elapsed", new Vector2(75 * 8 + 40, 40), Color.White);
                        spriteBatch.DrawString(font, time, new Vector2(75 * 8 + 40, 60), Color.White);
                        spriteBatch.DrawString(font, "Move", new Vector2(75 * 8 + 40, 100), Color.White);
                        spriteBatch.DrawString(font, moveCount.ToString(), new Vector2(75 * 8 + 40, 120), Color.White);
                        base.Draw(gameTime);
                        break;
                    }
                case (GameState.PlayGameAdv):
                    {
                        spriteBatch.Draw(Content.Load<Texture2D>("mainmenu"), new Vector2(0, 0), Color.White);
                        easy.Draw(spriteBatch, font);
                        medium.Draw(spriteBatch, font);
                        hard.Draw(spriteBatch, font);
                        break;
                    }
                case (GameState.CompGameAdv):
                    {
                        spriteBatch.Draw(Content.Load<Texture2D>("mainmenu"), new Vector2(0, 0), Color.White);
                        bfs.Draw(spriteBatch, font);
                        dfs.Draw(spriteBatch, font);
                        break;
                    }
                case (GameState.MainMenu):
                    {
                        spriteBatch.Draw(Content.Load<Texture2D>("mainmenu"), new Vector2(0, 0), Color.White);
                        playerMode.Draw(spriteBatch, font);
                        computerMode.Draw(spriteBatch, font);
                        help.Draw(spriteBatch, font);
                        about.Draw(spriteBatch, font);
                        break;
                    }
                case (GameState.About):
                    {
                        spriteBatch.Draw(Content.Load<Texture2D>("about"), new Vector2(0, 0), Color.White);
                        break;
                    }
                case (GameState.TutorialMenu):
                    {
                        spriteBatch.Draw(Content.Load<Texture2D>("help"), new Vector2(0, 0), Color.White);
                        break;
                    }
                case (GameState.Solving):
                    {
                        int i, j;
                        for (i = 0; i < field.GetMaxCol() + 2; i++)
                        {
                            for (j = 0; j < field.GetMaxRow() + 3; j++)
                            {
                                terrain.Position = new Vector2(i * (float)(terrain.GetTexture().Width / terrain.GetMaxFrameH() * terrain.Scale), j * (float)(terrain.GetTexture().Height / terrain.GetMaxFrameV() * terrain.Scale));
                                if (i % 2 == 0)
                                {
                                    if (j == 1)
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 0);
                                    }
                                    else if ((j != 0) && (j != field.GetMaxRow() + 2))
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 1);
                                    }
                                }
                                else
                                {
                                    if (j == 1)
                                    {
                                        terrain.Draw(this.spriteBatch, 1, 0);
                                    }
                                    else if ((j != 0) && (j != field.GetMaxRow() + 2))
                                    {
                                        terrain.Draw(this.spriteBatch, 1, 1);
                                    }
                                }

                                if (i == 0)
                                {
                                    if (j == 0)
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 2);
                                    }
                                    else if (j == field.GetMaxRow() + 2)
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 3);
                                    }
                                    else
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 4);
                                    }
                                }
                                else if (i == field.GetMaxCol() + 1)
                                {
                                    if (j == 3)
                                    {
                                        terrain.Draw(this.spriteBatch, 0, 5);
                                    }
                                    else if (j == 0)
                                    {
                                        terrain.Draw(this.spriteBatch, 1, 2);
                                    }
                                    else if (j == field.GetMaxRow() + 2)
                                    {
                                        terrain.Draw(this.spriteBatch, 1, 3);
                                    }
                                    else
                                    {
                                        terrain.Draw(this.spriteBatch, 1, 4);
                                    }
                                }
                                else if (j == 0)
                                {
                                    terrain.Draw(this.spriteBatch, 1, 5);
                                }
                                else if (j == field.GetMaxRow() + 2)
                                {
                                    terrain.Draw(this.spriteBatch, 1, 6);
                                }
                                if ((i == 6) && (j == 3))
                                {
                                    terrain.Draw(this.spriteBatch, 1, 7);
                                }
                            }
                        }
                        spriteBatch.Draw(Content.Load<Texture2D>("panel"), new Vector2(GraphicsDevice.PresentationParameters.BackBufferWidth - 225, 0), Color.White);
                        spriteBatch.DrawString(font, "Time Elapsed", new Vector2(75 * 8 + 40, 40), Color.White);
                        spriteBatch.DrawString(font, time, new Vector2(75 * 8 + 40, 60), Color.White);
                        spriteBatch.DrawString(font, "Move", new Vector2(75 * 8 + 40, 100), Color.White);
                        spriteBatch.DrawString(font, moveCount.ToString(), new Vector2(75 * 8 + 40, 120), Color.White);
                        spriteBatch.DrawString(font, "Press J", new Vector2(75 * 8 + 30, 200), Color.White);
                        spriteBatch.DrawString(font, "to slow down", new Vector2(75 * 8 + 30, 215), Color.White);
                        spriteBatch.DrawString(font, "Press H", new Vector2(75 * 8 + 30, 250), Color.White);
                        spriteBatch.DrawString(font, "to make it fast", new Vector2(75 * 8 + 30, 265), Color.White);
                        base.Draw(gameTime);
                        break;
                    }
            }
            spriteBatch.End();
        }

        private void playinput()
        {
            mouseState = Mouse.GetState();
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                float x = mouseState.X;
                float y = mouseState.Y;
                for (i = 0; i < movable.Count; i++)
                {
                    if (movable[i].isClick(x, y) && !kunci)
                    {
                        j = i;
                        kunci = true;
                    }
                }
                movable[j].moveTo(x, y);

            }
            if (kunci == true)
            {
                if (mouseState.LeftButton == ButtonState.Released)
                {
                    movable[j].resetmoveto();
                    kunci = false;
                    moveCount++;
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Enter))
            {
                gameState = GameState.Reset;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                gameState = GameState.MainMenu;
            }
             
            
        }
    }
}
